/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.wii.elpro.web.action;

import com.wii.elpro.dao.ProdukDAOInterface;
import com.wii.elpro.dao.hb.ProdukDAOImpl;
import com.wii.elpro.entity.Produk;
import com.wii.elpro.entity.ProdukMaterial;
import com.wii.elpro.entity.User;
import com.wii.elpro.web.form.ProductUpdateMaterialForm;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

/**
 *
 * @author hp02
 */
public class ProductPreUpdateMaterialAction extends Action {
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        HttpSession sess = request.getSession(true);
        User user = (User)sess.getAttribute("userSession");       
        if(user==null){
            return mapping.findForward("gagalLogin");            
        }  
        ActionForward af = null;
        ProdukDAOInterface dao = new ProdukDAOImpl();        
        ProductUpdateMaterialForm f = (ProductUpdateMaterialForm) form;
        int index = Integer.parseInt(request.getParameter("id"));
        String scope = request.getParameter("scope");
        ActionMessages messages = null;
        String id;
        if(sess.getAttribute("idProduct")!=null) {
            id = (String) sess.getAttribute("idProduct");
        } else {
            id = 0+"";
        }
        try {
            if(sess.getAttribute("idProduct")!=null) {
                if(scope!=null && scope.equalsIgnoreCase("session")) {
                    List<ProdukMaterial> materials = (List<ProdukMaterial>) sess.getAttribute("listMaterial");
                    f.setIndex(index);
                    f.setJumlah(materials.get(index).getJumlah());
                    f.setScope("session");
                } else {
                    Produk p = dao.getProduk(Long.parseLong((String) sess.getAttribute("idProduct")));
                    f.setIndex(index);
    //                System.out.println(sess.getAttribute("idProduct"));
    //                System.out.println(p.getNama());
    //                System.out.println("JUMLAH MATERIAL --------> "+p.getProdukMaterials().size());
                    f.setJumlah(p.getProdukMaterials().get(index).getJumlah());
                }
                messages = new ActionMessages();
                messages.add("customMessage", new ActionMessage("customMessage", "You can change quantity of material by filling the text box bellow."));
            } else {
                messages = new ActionMessages();
                messages.add("customMessage", new ActionMessage("customMessage", "Application is fail to retrieve your data. It may caused by missing required variable. Please contact my developer"));
            }
            af = new ActionForward(mapping.findForward("success"));
        } catch (Exception ex) {
            messages = new ActionMessages();
            messages.add("customMessage", new ActionMessage("customMessage", "Application is fail to retrieve your data. Please contact my developer"));
            ex.printStackTrace();
            af = new ActionForward(mapping.findForward("fail").getPath()+"?id="+id, true);
        }
        saveMessages(sess, messages);
        return af;
    }
}
